package cool.taomu.toolkit.test

import com.google.inject.AbstractModule
import com.google.inject.TypeLiteral
import cool.taomu.toolkit.TaomuGuice
import cool.taomu.toolkit.inter.IStoreKeyValue
import cool.taomu.toolkit.lmdb.LmdbProvider
import cool.taomu.toolkit.lmdb.LmdbStore
import java.nio.ByteBuffer
import org.lmdbjava.Env
import com.google.inject.Key
import org.junit.Test

class LmdbTest {

	@Test
	def void t() {
		var injector = TaomuGuice.createInjector(new AbstractModule() {
			override configure() {
				bind(new TypeLiteral<Env<ByteBuffer>>() {
				}).toProvider(LmdbProvider)
				bind(new TypeLiteral<IStoreKeyValue<String, byte[]>>() {
				}).to(LmdbStore);
			}
		});
		var kv = injector.getInstance(Key.get(new TypeLiteral<IStoreKeyValue<String, byte[]>>() {
				}))
		kv.put("a","Hello a".bytes)
		println(kv.containsKey("a"))
		println(new String(kv.get("a")))
		println(kv.containsKey("b"))
		println(kv.size)
		println("remove a")
		kv.remove("a")
		println(kv.size)
		println(kv.containsKey("a"))
		kv.put("b","Hello b".bytes)
		kv.put("c","Hello c".bytes)
		kv.put("d","Hello d".bytes)
		for(var i=0;i<1000;i++){
			kv.put("d"+i,("Hello d "+i).bytes)
		}
		println(new String(kv.get("b")))
		println(kv.containsKey("b"))
		println(kv.size)
		println("clear")
		kv.clear
		println(kv.containsKey("b"))
		kv.put("c","Hello c".bytes)
		println(new String(kv.get("c")))
		println(kv.size)
	}
}
